package com.wei.controller.web;

import com.wei.blog.common.exception.BlogException;
import com.wei.blog.common.help.UserHelp;
import com.wei.blog.common.utils.R;
import com.wei.entity.UserEntity;
import com.wei.service.IFollowerService;
import com.wei.service.UserService;
import com.wei.vo.FollowerVo;
import com.wei.vo.UserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/follower")
public class FollowerController {
    @Autowired
    private IFollowerService followerService;
    @Autowired
    private UserHelp userHelp;
    @Autowired
    private UserService userService;

    @ResponseBody
    @GetMapping("/click/{fid}")
    public R click(@PathVariable Integer fid) {
        if (userHelp.get() == null) {
            throw new BlogException("用户未登录！");
        }
        Integer userId = userHelp.get().getUid();
        followerService.click(fid, userId);
        return R.ok();
    }

    //跳转关注列表页面
    @GetMapping("/findFollowerListByUserId/{uid}")
    public String findFollowerListByUserId(@PathVariable Integer uid, ModelMap map) {
        List<FollowerVo> followerList = followerService.findFollowerListByUserId(uid, 1);

        UserEntity userEntity = userService.findUserById(uid);

        FollowerVo followerVo = new FollowerVo();
        followerVo.setUserId(userEntity.getId());
        followerVo.setUsername(userEntity.getUsername());

        UserToken loginUser = userHelp.get();

        map.put("followerVo", followerVo);
        map.put("loginUser", loginUser);
        map.put("followerList", followerList);
        return "site/followee";
    }

    //粉丝列表
    @GetMapping("/findFansListByUserId/{uid}")
    public String findFansListByUserId(@PathVariable Integer uid, ModelMap map) {
        List<FollowerVo> fansList = followerService.findFollowerListByUserId(uid, 2);
        UserEntity userEntity = userService.findUserById(uid);

        FollowerVo followerVo = new FollowerVo();
        followerVo.setUserId(userEntity.getId());
        followerVo.setUsername(userEntity.getUsername());

        UserToken loginUser = userHelp.get();

        map.put("followerVo", followerVo);
        map.put("loginUser", loginUser);
        map.put("fansList", fansList);
        return "site/fans";
    }


}
